package xjay.ai.emotionalsupport.tools;

import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;
import xjay.ai.emotionalsupport.constant.FileConstant;

@Slf4j
@Component
public class FileTool {
    private final String FILE_DIR = FileConstant.FILE_SAVE_DIR + "/file";


    @Tool(description = "Read content from a file")
    public String readFile(@ToolParam(description = "file name for reading") String path) {
        path = FILE_DIR + "/" + path;
        log.info("FileTool:readFile中读取文件");
        return FileUtil.readUtf8String(path);
    }


    @Tool(description = "write content to a file")
    public void writeFile(@ToolParam(description = "file name for writing") String path, @ToolParam(description = "content to write") String content) {
        path = FILE_DIR + "/" + path;
            FileUtil.mkdir(FILE_DIR);
        FileUtil.writeUtf8String(path, content); //目标文件不存在，自动创建；目标文件存在，覆盖。
        log.info("FileTool:writeFile中写入文件");
    }
}
